home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / plot2d / g2dint.jav < prev    next >
Encoding:
Text File  |  1996-01-11  |  6.8 KB  |  235 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.net.URL;
  4. import java.util.*;
  5.  
  6. /*************************************************************************
  7. **
  8. **    Class  G2Dint
  9. **                                              Version 1.0   October 1995
  10. **
  11. **************************************************************************
  12. **    Copyright (C) 1995 Leigh Brookshaw
  13. **
  14. **    This program is free software; you can redistribute it and/or modify
  15. **    it under the terms of the GNU General Public License as published by
  16. **    the Free Software Foundation; either version 2 of the License, or
  17. **    (at your option) any later version.
  18. **
  19. **    This program is distributed in the hope that it will be useful,
  20. **    but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. **    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. **    GNU General Public License for more details.
  23. **
  24. **    You should have received a copy of the GNU General Public License
  25. **    along with this program; if not, write to the Free Software
  26. **    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. **************************************************************************
  28. **
  29. **    This class is an extension of Graph2D class.
  30. **    It adds interactive selection of the plotting range.
  31. **
  32. **    MouseDown: Starts the range selection
  33. **    MouseDrag: Drag out a rectangular range selection
  34. **    MouseUp:   Replot with modified plotting range.
  35. **
  36. **    Shift MouseUp: reset back to default range.
  37. **
  38. *************************************************************************/
  39.  
  40.  
  41. class G2Dint extends Graph2D {
  42.  
  43. /*
  44. **    Set to true when a rectangle is being dragged out by the mouse
  45. */
  46.       private boolean drag = false;
  47. /*
  48. **    Button Down position
  49. */
  50.       private int x0,y0;
  51. /*
  52. **    Button Drag position
  53. */
  54.       private int  x1,y1;
  55. /*
  56. **    Previous Button Drag position
  57. */
  58.       private int x1old, y1old;
  59.  
  60. /*
  61. **    Attached Axis which must be registered with this class
  62. **    These are the axis used to find the drag range.
  63. **    If no axis are registered no mouse drag.
  64. */
  65.       private Axis xaxis;
  66.       private Axis yaxis;
  67.  
  68.  
  69.  
  70.  
  71. /*
  72. **    Attach xaxis to be used for the drag scaling
  73. */
  74.       public Axis createXAxis() {
  75.          xaxis = super.createAxis(Axis.BOTTOM);
  76.          return xaxis;
  77.       }
  78. /*
  79. **    Attach yaxis to be used for the drag scaling
  80. */
  81.       public Axis createYAxis() {
  82.          yaxis = super.createAxis(Axis.LEFT);
  83.          return yaxis;
  84.       }
  85.  
  86. /*
  87. **  Override the normal Graph2D update method with this new one
  88. */
  89.     public void update(Graphics g) {
  90.           Rectangle r = bounds();
  91.           Color c = g.getColor();
  92.  
  93.           if(drag) {
  94. /*
  95. **         Drag out the new box.
  96. **         Use drawLine instead of drawRect to avoid problems
  97. **         when width and heights become negative. Seems drawRect
  98. **         can't handle it!
  99. */
  100.  
  101.            g.setColor(getBackground());
  102.            g.drawLine(x0, y0, x1old, y0);
  103.            g.drawLine(x1old, y0, x1old, y1old);
  104.            g.drawLine(x1old, y1old, x0, y1old);
  105.            g.drawLine(x0, y1old, x0, y0);
  106.            g.setColor(getForeground());
  107.            g.drawLine(x0, y0, x1, y0);
  108.            g.drawLine(x1, y0, x1, y1);
  109.            g.drawLine(x1, y1, x0, y1);
  110.            g.drawLine(x0, y1, x0, y0);
  111.  
  112.  
  113.            g.setColor(c);
  114.  
  115.            x1old = x1;
  116.            y1old = y1;
  117.  
  118.            return;
  119.            }
  120.  
  121.           if( clearAll ) {
  122.              g.setColor(getBackground());
  123.              g.fillRect(r.x,r.y,r.width,r.height);
  124.              g.setColor(c);
  125.           }
  126.           if( paintAll ) paint(g);
  127.     }
  128.  
  129. /*
  130. ** Handle the mouse events
  131. */
  132.     public boolean handleEvent(Event e) {
  133.  
  134.         if( xaxis == null || yaxis == null ) return false;
  135.  
  136.         switch (e.id) {          
  137.           case Event.MOUSE_DOWN:
  138.  
  139.                 x0 = e.x;
  140.                 y0 = e.y;
  141.  
  142.                 if( !e.shiftDown() ) { 
  143.                           drag = true; 
  144.                           x1old = x0;
  145.                           y1old = y0;
  146.                 }
  147.  
  148.                 if(x0 < datarect.x) x0 = datarect.x;
  149.                 else
  150.                 if(x0 > datarect.x + datarect.width ) 
  151.                     x0 = datarect.x + datarect.width;
  152.  
  153.                 if(y0 < datarect.y) y0 = datarect.y;
  154.                 else
  155.                 if(y0 > datarect.y + datarect.height ) 
  156.                     y0 = datarect.y + datarect.height;
  157.  
  158.  
  159.                 return true;
  160.           case Event.MOUSE_UP:
  161.                 x1   = e.x;
  162.                 y1   = e.y;
  163.  
  164.                 drag = false;
  165.  
  166.                 if( e.shiftDown() ) {
  167.                     xaxis.minimum = xaxis.getDataMin();
  168.                     xaxis.maximum = xaxis.getDataMax();
  169.                     yaxis.minimum = yaxis.getDataMin();
  170.                     yaxis.maximum = yaxis.getDataMax();
  171.                     repaint();
  172.                     return true;
  173.                 }
  174.  
  175.                 if(x1 < datarect.x) x1 = datarect.x;
  176.                 else
  177.                 if(x1 > datarect.x + datarect.width ) 
  178.                     x1 = datarect.x + datarect.width;
  179.  
  180.                 if(y1 < datarect.y) y1 = datarect.y;
  181.                 else
  182.                 if(y1 > datarect.y + datarect.height ) 
  183.                     y1 = datarect.y + datarect.height;
  184.  
  185.  
  186.                 if( Math.abs(x0-x1) > 5 &&  Math.abs(y0-y1) > 5 ) {
  187.                    if(x0 < x1 ) {                
  188.                       xaxis.minimum = xaxis.getDouble(x0);
  189.                       xaxis.maximum = xaxis.getDouble(x1);
  190.                    } else {
  191.                       xaxis.maximum = xaxis.getDouble(x0);
  192.                       xaxis.minimum = xaxis.getDouble(x1);
  193.                    }
  194.  
  195.                    if(y0 >y1 ) {                
  196.                       yaxis.minimum = yaxis.getDouble(y0);
  197.                       yaxis.maximum = yaxis.getDouble(y1);
  198.                    } else {
  199.                       yaxis.maximum = yaxis.getDouble(y0);
  200.                       yaxis.minimum = yaxis.getDouble(y1);
  201.                    }
  202.  
  203.                    repaint();
  204.                  }
  205.                 return true;
  206.  
  207.            case Event.MOUSE_DRAG:
  208.                 
  209.  
  210.                 x1   = e.x;
  211.                 y1   = e.y;
  212.  
  213.                 if(x1 < datarect.x) x1 = datarect.x;
  214.                 else
  215.                 if(x1 > datarect.x + datarect.width ) 
  216.                     x1 = datarect.x + datarect.width;
  217.  
  218.                 if(y1 < datarect.y) y1 = datarect.y;
  219.                 else
  220.                 if(y1 > datarect.y + datarect.height ) 
  221.                     y1 = datarect.y + datarect.height;
  222.  
  223.                 repaint();
  224.  
  225.                 return true;
  226.            }
  227.  
  228.            return false;
  229.        
  230.          }
  231.  
  232.  
  233.  
  234. }
  235.